home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Science / µSim 1.0 folder / source / myMemory.c < prev    next >
Encoding:
Text File  |  1995-01-14  |  5.5 KB  |  184 lines  |  [TEXT/MMCC]

  1. /*
  2. Copyright © 1993,1994 by Fabrizio Oddone
  3. ••• ••• ••• ••• ••• ••• ••• ••• ••• •••
  4. This source code is distributed as freeware: you can copy, exchange, modify this
  5. code as you wish. You may include this code in any kind of application: freeware,
  6. shareware, or commercial, provided that full credits are given.
  7. You may not sell or distribute this code for profit.
  8. */
  9.  
  10. //#pragma load "MacDump"
  11.  
  12. #include    "UtilsSys7.h"
  13. #include    "Globals.h"
  14. #include    "Animation.h"
  15. #include    "Disasm.h"
  16. #include    "Dump.h"
  17. #include    "myMemory.h"
  18. #include    "Registers.h"
  19. #include    "SimUtils.h"
  20.  
  21. #if defined(FabSystem7orlater)
  22.  
  23. #pragma segment Rare
  24.  
  25. /* myOpenFile: opens a dump file */
  26.  
  27. OSErr    myOpenFile(FSSpec *theFile, Ptr where, Size howmuch)
  28. {
  29. ParamBlockRec    myPB;
  30. EventRecord    dummyEv;
  31. short    myFileRefN;
  32. register OSErr    err;
  33.  
  34. SetCursor(*gWatchHandle);
  35. if ((err = FSpOpenDFCompat(theFile, fsRdPerm, &myFileRefN)) == noErr) {
  36.     myPB.ioParam.ioCompletion = nil;
  37.     myPB.ioParam.ioRefNum = myFileRefN;
  38.     myPB.ioParam.ioBuffer = where;
  39.     myPB.ioParam.ioReqCount = howmuch;
  40.     myPB.ioParam.ioPosMode = fsFromStart;
  41.     myPB.ioParam.ioPosOffset = 0L;
  42.     (void)PBReadAsync(&myPB);
  43.     while (myPB.ioParam.ioResult > 0) {
  44.         SystemTask();
  45.         (void)EventAvail(everyEvent, &dummyEv);
  46.         }
  47.     err = myPB.ioParam.ioResult;
  48.     (void)FSClose(myFileRefN);
  49.     if (where == (Ptr)&gRegs)
  50.         ChangedAllRegisters();
  51.     else {
  52.         InvalDump();
  53.         InvalDisasm();
  54.         }
  55.     }
  56. SetCursor(&qd.arrow);
  57. return(err);
  58. }
  59.  
  60. /* mySaveFile: saves a dump file to disk */
  61.  
  62. OSErr mySaveFile(Ptr where, Size howmuch, OSType fType, short prompt, short defName)
  63. {
  64. StandardFileReply    mySFR;
  65. Str255    tempFName;
  66. ParamBlockRec    myPB;
  67. EventRecord    dummyEv;
  68. FSSpec    thisFSSpec;
  69. long    thisDirID;
  70. short    thisVRefNum, tmpFRefN;
  71. register SignedByte    state1, state2;
  72. register Handle    sH1 = (Handle)GetString(prompt);
  73. register Handle    sH2 = (Handle)GetString(defName);
  74. register OSErr    err;
  75.  
  76. err = noErr;
  77. state1 = WantThisHandleSafe(sH1);
  78. state2 = WantThisHandleSafe(sH2);
  79. StandardPutFile((ConstStr255Param)*sH1, (ConstStr255Param)*sH2, &mySFR);
  80. HSetState(sH2, state2);
  81. HSetState(sH1, state1);
  82. if (mySFR.sfGood) {
  83.     SetCursor(*gWatchHandle);
  84.     myPB.ioParam.ioCompletion = nil;
  85.     myPB.ioParam.ioBuffer = where;
  86.     myPB.ioParam.ioReqCount = howmuch;
  87.     myPB.ioParam.ioPosMode = fsFromStart;
  88.     if (mySFR.sfReplacing)
  89.         if (0)    /* test if file locked (FSpExchange does not) */
  90.             err = fLckdErr;
  91.         else {
  92.             MyNumToString(TickCount(), tempFName);
  93.             if ((err = FindFolder(mySFR.sfFile.vRefNum, kTemporaryFolderType,
  94.                                 kCreateFolder, &thisVRefNum, &thisDirID)) == noErr) {
  95.                 (void)FSMakeFSSpecCompat(thisVRefNum, thisDirID, (ConstStr255Param)&tempFName, &thisFSSpec);
  96.                 if ((err = FSpCreateCompat(&thisFSSpec, kFCR_MINE, fType,
  97.                                     mySFR.sfScript)) == noErr)
  98.                     if ((err = FSpOpenDFCompat(&thisFSSpec, fsWrPerm, &tmpFRefN)) == noErr) {
  99.                         myPB.ioParam.ioRefNum = tmpFRefN;
  100.                         myPB.ioParam.ioPosOffset = 0L;
  101.                         (void)PBWriteAsync(&myPB);    // in temporary file
  102.                         while (myPB.ioParam.ioResult > 0) {
  103.                             SystemTask();
  104.                             (void)EventAvail(everyEvent, &dummyEv);
  105.                             }
  106.                         (void)FSClose(tmpFRefN);
  107.                         if ((err = myPB.ioParam.ioResult) == noErr) {
  108.                             if ((err = AddSTRRes2Doc(&thisFSSpec, kFCR_MINE,
  109.                                                     fType, kSTR_ApplicationName, mySFR.sfScript))
  110.                                     == noErr)
  111.                                 if ((err = FSpExchangeFilesCompat(&thisFSSpec, &(mySFR.sfFile)))
  112.                                         == noErr)
  113.                                     err = FSpDeleteCompat(&thisFSSpec);
  114.                             }
  115.                         else {
  116.                             (void)FSpDeleteCompat(&thisFSSpec);    // the temporary one
  117.                             if (err == dskFulErr) {
  118.                                 if (CautionAlert_UPP(kALRT_NOSAFESAVE, myStdFilterProc) == ok) {
  119.                                     if ((err = FSpOpenDFCompat(&(mySFR.sfFile), fsWrPerm,
  120.                                                         &tmpFRefN)) == noErr) {
  121.                                         myPB.ioParam.ioRefNum = tmpFRefN;
  122.                                         myPB.ioParam.ioPosOffset = 0L;
  123.                                         (void)PBWriteAsync(&myPB);
  124.                                         while (myPB.ioParam.ioResult > 0) {
  125.                                             SystemTask();
  126.                                             (void)EventAvail(everyEvent, &dummyEv);
  127.                                             }
  128.                                         (void)FSClose(tmpFRefN);
  129.                                         err = myPB.ioParam.ioResult;
  130.                                         }
  131.                                     }
  132.                                 else err = noErr; /* user clicked cancel in alert */
  133.                                 }
  134.                             }
  135.                         }
  136.                 }
  137.             }
  138. /* not replacing an existing file */
  139.     else if ((err = FSpCreateCompat(&(mySFR.sfFile), kFCR_MINE, fType, mySFR.sfScript)) == noErr)
  140.         if ((err = FSpOpenDFCompat(&(mySFR.sfFile), fsWrPerm, &tmpFRefN)) == noErr) {
  141.             myPB.ioParam.ioRefNum = tmpFRefN;
  142.             myPB.ioParam.ioPosOffset = 0L;
  143.             (void)PBWriteAsync(&myPB);
  144.             while (myPB.ioParam.ioResult > 0) {
  145.                 SystemTask();
  146.                 (void)EventAvail(everyEvent, &dummyEv);
  147.                 }
  148.             (void)FSClose(tmpFRefN);
  149.             if ((err = myPB.ioParam.ioResult) == noErr)
  150.                 err = AddSTRRes2Doc(&(mySFR.sfFile), kFCR_MINE, fType,
  151.                                     kSTR_ApplicationName, mySFR.sfScript);
  152.             }
  153.     SetCursor(&qd.arrow);
  154.     }
  155. return err;
  156. }
  157.  
  158.  
  159. OSErr    OpenProcessorState(FSSpecPtr theFile)
  160. {
  161. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  162. register OSErr    err;
  163.  
  164. if (noErr == (err = myOpenFile(theFile, (Ptr)&state, sizeof(state)))) {
  165.     BlockMoveData((Ptr)&state, (Ptr)&gParts, sizeof(gParts));
  166.     BlockMoveData((Ptr)(state + kNUMOFPARTS), (Ptr)&gRegs, sizeof(state) - sizeof(gParts));
  167.     ChangedAllRegisters();
  168.     ChangedAllBoxes();
  169.     }
  170. return err;
  171. }
  172.  
  173. OSErr    SaveProcessorState(void)
  174. {
  175. short    state[kNUMOFSTATEPARTS];    /* the registers & editable parts of my machine */
  176.  
  177. BlockMoveData((Ptr)&gParts, (Ptr)&state, sizeof(gParts));
  178. BlockMoveData((Ptr)&gRegs, (Ptr)(state + kNUMOFPARTS), sizeof(state) - sizeof(gParts));
  179. return mySaveFile((Ptr)&state, sizeof(state), kFTY_REG, kSTR_REGSPROMPT, kSTR_REGSDEFNAME);
  180. }
  181.  
  182. #endif
  183.  
  184.